iT邦幫忙

0

C# web GridView 匯出成EXCEL (with BOM) VS. (without BOM)

  • 分享至 

  • xImage
  •  

把GridView資料匯成EXCEL

UI畫面
https://ithelp.ithome.com.tw/upload/images/20190927/20106764odHJ61aRkB.png
without bom匯出 : 中文會亂碼
https://ithelp.ithome.com.tw/upload/images/20190927/20106764dyOuncO9ZL.png
with bom匯出 : 中文顯示正常
https://ithelp.ithome.com.tw/upload/images/20190927/201067648tlDmCaak5.png

程式如下
aspx

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
    </div>
        <asp:GridView ID="GridView1" runat="server">
        </asp:GridView>
        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" style="height: 21px" Text="nobom" />
        <asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="withbom" />
    </form>
</body>
</html>

cs

        protected void Page_Load(object sender, EventArgs e)
        {
            System.Data.DataTable dt = new System.Data.DataTable();
            dt.Columns.Add("col1");
            dt.Columns.Add("col2");

            System.Data.DataRow row1 = dt.NewRow();
            row1[0] = "國文";
            row1[1] = "99";
            dt.Rows.Add(row1);
            System.Data.DataRow row2 = dt.NewRow();
            row2[0] = "英文";
            row2[1] = "100";
            dt.Rows.Add(row2);

            GridView1.DataSource = dt;
            GridView1.DataBind();
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            nobom();
        }
        protected void Button2_Click(object sender, EventArgs e)
        {
            withbom();
        }
        private void nobom()
        {
            Response.Buffer = true;
            Response.Charset = "UTF-8"; //設定字集
            Response.AppendHeader("Content-Disposition", "attachment;filename=nobom.xls"); //filename 可自定
            Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8");
            Response.ContentType = "application/ms-excel "; //內容型態設為Excel

            System.IO.StringWriter objStringWriter = new System.IO.StringWriter();
            System.Web.UI.HtmlTextWriter objHtmlTextWriter = new System.Web.UI.HtmlTextWriter(objStringWriter);
            this.GridView1.RenderControl(objHtmlTextWriter);
            
            Response.Write(objStringWriter.ToString());
            Response.End();
        }
        private void withbom()
        {
            Response.Buffer = true;
            Response.Charset = "UTF-8"; //設定字集
            Response.AppendHeader("Content-Disposition", "attachment;filename=withbom.xls"); //filename 可自定
            Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8");
            Response.ContentType = "application/ms-excel "; //內容型態設為Excel

            System.IO.StringWriter objStringWriter = new System.IO.StringWriter();
            System.Web.UI.HtmlTextWriter objHtmlTextWriter = new System.Web.UI.HtmlTextWriter(objStringWriter);
            this.GridView1.RenderControl(objHtmlTextWriter);

            System.IO.StreamWriter sw = new System.IO.StreamWriter(Response.OutputStream, System.Text.Encoding.UTF8);//這樣一筆的時侯,就不會亂碼了
            sw.Write(objStringWriter.ToString());
            sw.Close();
             
            Response.End();
        }

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言